/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.web.online;

import com.wallellen.core.sql.result.DataResult;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by walle on 12/9/15.
 */
public class OnlineUserOp implements OnlineUserOpInterface {
    private String ip;
    private String loginName;
    private HttpSession httpSession;
    private Map<String, Object> strutsSession;

    public boolean doUserDownLine(String loginName) {
        OnlineUserOpInterface.onlineUserTable.remove(loginName);
        return true;
    }

    public DataResult findOnlineUser() {
        List list = new ArrayList();
        for (String key : OnlineUserOpInterface.onlineUserTable.keySet()) {
            Date date = (Date) ((Map) OnlineUserOpInterface.onlineUserTable.get(key)).get("TIME");

            Date visitdate = (Date) ((Map) OnlineUserOpInterface.onlineUserTable.get(key)).get("LOGINTIME");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat curentDayf = new SimpleDateFormat("yyyy-MM-dd");

            Date curentTime = new Date();

            Calendar curentC = Calendar.getInstance();
            curentC.setTime(curentTime);

            Calendar curentV = Calendar.getInstance();
            curentV.setTime(date);

            Calendar curentl = Calendar.getInstance();
            curentl.setTime(visitdate);

            long timeMillis = (curentC.getTimeInMillis() - curentV.getTimeInMillis()) / 60000L;

            long visitMillis = (curentC.getTimeInMillis() - curentl.getTimeInMillis()) / 60000L;

            if (timeMillis > 60L) {
                OnlineUserOpInterface.onlineUserTable.remove(key);
            } else {
                Map map = new HashMap();
                map.put("LNAME", key);
                map.put("VISITTIME", Long.valueOf(visitMillis));
                map.put("TIME", sdf.format(date).replace(curentDayf.format(new Date()), "今天"));

                map.put("USEROBJ", ((Map) OnlineUserOpInterface.onlineUserTable.get(key)).get("USEROBJ"));

                map.put("LOGINTIME", sdf.format(((Map) OnlineUserOpInterface.onlineUserTable.get(key)).get("LOGINTIME")).replace(curentDayf.format(new Date()), "今天"));

                map.put("IP", ((Map) OnlineUserOpInterface.onlineUserTable.get(key)).get("IP"));

                list.add(map);
            }
        }
        DataResult result = DataResult.getInstance(list, list.size(), 1, list.size());

        return result;
    }

    public void userLoginHandle(Object user) {
        if (((this.httpSession != null) || (this.strutsSession != null)) && (this.loginName != null) && (this.ip != null)) {
            Map userMap = new HashMap();
            userMap.put("IP", this.ip);
            userMap.put("TIME", new Date());
            userMap.put("LNAME", this.loginName);
            userMap.put("LOGINTIME", new Date());
            userMap.put("USEROBJ", user);

            OnlineUserOpInterface.onlineUserTable.put(this.loginName, userMap);
        } else {
            throw new RuntimeException("参数错误");
        }
    }

    public void userVisitHandle() {
        if (((this.httpSession != null) || (this.strutsSession != null)) && (this.loginName != null) && (this.ip != null)) {
            Map userMap = (Map) OnlineUserOpInterface.onlineUserTable.get(this.loginName);

            if (userMap == null) {
                clearSessionUser();
                return;
            }
            userMap.put("TIME", new Date());
            if (!userMap.get("IP").equals(this.ip)) {
                clearSessionUser();
                return;
            }

        } else {
            throw new RuntimeException("参数错误");
        }
    }

    private void clearSessionUser() {
        if (this.httpSession != null) {
            this.httpSession.removeAttribute("USEROBJ");
        }
        if (this.strutsSession != null) {
            this.strutsSession.put("USEROBJ", null);
            this.strutsSession.clear();
        }
    }

    public String getIp() {
        return this.ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public HttpSession getHttpSession() {
        return this.httpSession;
    }

    public void setHttpSession(HttpSession httpSession) {
        this.httpSession = httpSession;
    }

    public Map<String, Object> getStrutsSession() {
        return this.strutsSession;
    }

    public void setStrutsSession(Map<String, Object> strutsSession) {
        this.strutsSession = strutsSession;
    }

    public String getLoginName() {
        return this.loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public static OnlineUserOpInterface getInstance(String ip, String loginName, HttpSession httpSession) {
        OnlineUserOp obj = new OnlineUserOp();
        obj.setHttpSession(httpSession);
        obj.setLoginName(loginName);
        obj.setIp(ip);
        return obj;
    }

    public static OnlineUserOpInterface getInstance(String ip, String loginName, Map<String, Object> strutsSession) {
        OnlineUserOp obj = new OnlineUserOp();
        obj.setIp(ip);
        obj.setLoginName(loginName);
        obj.setStrutsSession(strutsSession);
        return obj;
    }

    public static OnlineUserOpInterface getInstance() {
        OnlineUserOp obj = new OnlineUserOp();
        return obj;
    }
}
